/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package camps;

import db.NWDB;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author gjun
 */
public class CalendarBefore {
    
    public CalendarBefore() {
    }
    
    public String execute() throws Exception {

        HttpServletRequest request=ServletActionContext.getRequest(); 
        
        Calendar c = Calendar.getInstance();
        int thisYear = c.get(Calendar.YEAR);                //取得今年
        int thisMonth = c.get(Calendar.MONTH)+1;            //取得今月
        int thisDay = c.get(Calendar.DAY_OF_MONTH);         //取得今日
        int thisMonthDay = 0;                                   //取得今月有幾天
        if(thisMonth==1 || thisMonth==3 || thisMonth==5 || thisMonth==7 || thisMonth==8 || thisMonth==10 || thisMonth==12){  
            thisMonthDay = 31;           
        }else if(thisMonth==4 || thisMonth==6 || thisMonth==9 || thisMonth==11){  
            thisMonthDay = 30;           
        }else if(thisMonth == 2){  
            if(((thisYear % 4 == 0) && (thisYear % 100 != 0)) || (thisYear % 400 == 0))
                thisMonthDay = 29;               
            else  
                thisMonthDay = 28;               
        }             
        int minusDay = thisMonthDay - thisDay;  //今月的天數 - 今天幾號
        int lockDay = 7;                    //要鎖住的天數
        int plusDay = lockDay - minusDay;   //下個月要鎖住的天數
        
        int year;           //接收form傳來的年
        int month;          //接收form傳來的月
        if(request.getParameter("year") == null || request.getParameter("month") == null){
            if(plusDay>=0){
                year = thisYear;
                month = thisMonth+1;
            }else{
                year = thisYear;
                month = thisMonth;
            }           
        }else{
            year = Integer.parseInt(request.getParameter("year"));
            month = Integer.parseInt(request.getParameter("month"));
            boolean limitA = year <= thisYear && month < thisMonth;     //年月的上限
            boolean limitB = year > thisYear && month > thisMonth;      //年月的下限
            if(month > 13){
                year++;
                month = 1;
            }
            if(month < 1){
                year--;
                month = 12;
            }
            if(limitA){
                year = thisYear + 1;
                month = thisMonth;
            }
            if(limitB){
                year = thisYear;
                month = thisMonth;
            }
        }

        c.set(Calendar.YEAR, year);             //利用年來控制日曆
        c.set(Calendar.MONTH, month-1);           //利用月來控制日曆
        c.set(Calendar.DAY_OF_MONTH, 1);            //固定抓這個月1號
        year = c.get(Calendar.YEAR);                    //幾年
        month = c.get(Calendar.MONTH) + 1;              //幾月，一月 = 0
        int weekDay = c.get(Calendar.DAY_OF_WEEK) - 1;      //禮拜幾，星期天 = 0   
        int monthDay = 0;              //一個月有幾天
        if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){  
            monthDay = 31;           
        }else if(month==4 || month==6 || month==9 || month==11){  
            monthDay = 30;           
        }else if(month == 2){  
            if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
                monthDay = 29;               
            else  
                monthDay = 28;               
        }
        
        int count = 1;
        String[][] days = new String[6][7];
        for(int i = 0; i < 6; i ++)         //清空日曆
            for(int j = 0; j < 7; j++)
                days[i][j] = "";
        for(int i = 0; i < 6; i ++){        //填上日曆
            for(int j = 0; j < 7; j++){            
                if(i == 0 && j == weekDay){
                    days[i][j] = String.valueOf(count++);
                    if(thisDay == count){
                    }
                }else if(count > 1 && count <= monthDay){
                    days[i][j] = String.valueOf(count++);
                    if(thisDay == count){
                    }
                }
            }
        } 

        String[][] date = new String[6][7]; //取得日期ex:2014/9/22
        for(int i = 0; i < 6; i ++){        
            for(int j = 0; j < 7; j++){   
                if(days[i][j] != "")
                    date[i][j] = year + "/" + month + "/" + days[i][j];
                else
                    date[i][j] = "";
            }
        }
        getProductcount(days, date, year, month, thisMonthDay, lockDay, minusDay, plusDay);  //取得每日剩餘人數
        
        request.setAttribute("year", year);
        request.setAttribute("month", month);
        request.setAttribute("days", days);
        request.setAttribute("date", date);
        
        return "success";
    }
    
    //取得每日剩餘人數，year為傳來的年，month為傳來的月，lockDay為要鎖住的天數
    private void getProductcount(String[][] days, String[][] date, int year, int month, 
            int thisMonthDay, int lockDay, int minusDay, int plusDay) throws Exception{
        Calendar c = Calendar.getInstance();
        int thisYear = c.get(Calendar.YEAR);                //取得今年
        int thisMonth = c.get(Calendar.MONTH)+1;            //取得今月
        int thisDay = c.get(Calendar.DAY_OF_MONTH);         //取得今日
        
        NWDB db = new NWDB();
        String[][] aa = new String[6][7];
        String[][] bb = new String[6][7];
        String[][] cc = new String[6][7];
        boolean now = year == thisYear && month == thisMonth;
        boolean next = year >= thisYear;
        int count = 0;
        if(year >= thisYear){
            if(minusDay >= lockDay){
                if(now){     //這個月                  
                    for(int i = 0; i < 6; i ++){        
                        for(int j = 0; j < 7; j++){     
                            if(days[i][j] != "")
                                count++;
                            
                            if(days[i][j] != "" && count > (thisDay + lockDay)){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">A營地  剩餘%s</a>"
                                                , date[i][j],"A",db.getProductCount(date[i][j], "A")));
                                aa[i][j] = sb.toString();
                            }else if(days[i][j] != "" && count <= (thisDay + lockDay)){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                aa[i][j] = sb.toString();
                            }else
                                aa[i][j] = "";
                            if(days[i][j] != "" && count > (thisDay + lockDay)){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">B營地  剩餘%s</a>"
                                                , date[i][j],"B",db.getProductCount(date[i][j], "B")));
                                bb[i][j] = sb.toString();
                            }else if(days[i][j] != "" && count <= (thisDay + lockDay)){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                bb[i][j] = sb.toString();
                            }else
                                bb[i][j] = "";
                            if(days[i][j] != "" && count > (thisDay + lockDay)){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">C營地  剩餘%s</a>"
                                                , date[i][j],"C",db.getProductCount(date[i][j], "C")));
                                cc[i][j] = sb.toString();
                            }else if(days[i][j] != ""&& count <= (thisDay + lockDay)){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                cc[i][j] = sb.toString();
                            }else
                                cc[i][j] = "";
                        }
                    }
                }else if(next){     //下個月之後
                    for(int i = 0; i < 6; i ++){        
                        for(int j = 0; j < 7; j++){            
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">A營地  剩餘%s</a>"
                                                , date[i][j],"A",db.getProductCount(date[i][j], "A")));
                                aa[i][j] = sb.toString();
                            }else
                                aa[i][j] = "";
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">B營地  剩餘%s</a>"
                                                , date[i][j],"B",db.getProductCount(date[i][j], "B")));
                                bb[i][j] = sb.toString();
                            }else
                                bb[i][j] = "";
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">C營地  剩餘%s</a>"
                                                , date[i][j],"C",db.getProductCount(date[i][j], "C")));
                                cc[i][j] = sb.toString();
                            }else
                                cc[i][j] = "";
                        }
                    }
                }
            }else{
                if(now){     //這個月
                    for(int i = 0; i < 6; i ++){        
                        for(int j = 0; j < 7; j++){            
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                aa[i][j] = sb.toString();
                            }else
                                aa[i][j] = "";
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                bb[i][j] = sb.toString();
                            }else
                                bb[i][j] = "";
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                cc[i][j] = sb.toString();
                            }else
                                cc[i][j] = "";
                        }
                    }
                }else if(year == thisYear && month == thisMonth+1){     //下個月
                    for(int i = 0; i < 6; i ++){        
                        for(int j = 0; j < 7; j++){  
                            if(days[i][j] != "")
                                count++;
                            
                            if(days[i][j] != "" && count > plusDay){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">A營地  剩餘%s</a>"
                                                , date[i][j],"A",db.getProductCount(date[i][j], "A")));
                                aa[i][j] = sb.toString();
                            }else if(days[i][j] != "" && count <= plusDay){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                aa[i][j] = sb.toString();
                            }else
                                aa[i][j] = "";
                            if(days[i][j] != "" && count > plusDay){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">B營地  剩餘%s</a>"
                                                , date[i][j],"B",db.getProductCount(date[i][j], "B")));
                                bb[i][j] = sb.toString();
                            }else if(days[i][j] != "" && count <= plusDay){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                bb[i][j] = sb.toString();
                            }else
                                bb[i][j] = "";
                            if(days[i][j] != "" && count > plusDay){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">C營地  剩餘%s</a>"
                                                , date[i][j],"C",db.getProductCount(date[i][j], "C")));
                                cc[i][j] = sb.toString();
                            }else if(days[i][j] != "" && count <= plusDay){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                cc[i][j] = sb.toString();
                            }else
                                cc[i][j] = "";
                        }
                    }
                }else{      //下下個月之後
                    for(int i = 0; i < 6; i ++){        
                        for(int j = 0; j < 7; j++){            
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">A營地  剩餘%s</a>"
                                                , date[i][j],"A",db.getProductCount(date[i][j], "A")));
                                aa[i][j] = sb.toString();
                            }else if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                aa[i][j] = sb.toString();
                            }else
                                aa[i][j] = "";
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">B營地  剩餘%s</a>"
                                                , date[i][j],"B",db.getProductCount(date[i][j], "B")));
                                bb[i][j] = sb.toString();
                            }else if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                bb[i][j] = sb.toString();
                            }else
                                bb[i][j] = "";
                            if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append(String.format("<a href=\"/JavaCamping/camp/camplist?campDate=%s&campID=%s\">C營地  剩餘%s</a>"
                                                , date[i][j],"C",db.getProductCount(date[i][j], "C")));
                                cc[i][j] = sb.toString();
                            }else if(days[i][j] != ""){
                                StringBuilder sb=new StringBuilder();
                                sb.append("超過預定時間");
                                cc[i][j] = sb.toString();
                            }else
                                cc[i][j] = "";
                        }
                    }
                }
            }   
        }
        
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setAttribute("aa", aa);
        request.setAttribute("bb", bb);
        request.setAttribute("cc", cc);
        db.dbClose();
    }
    
}
